#include <iostream>
#include <thread>
#include <chrono>
#include <unistd.h>

using namespace std;

class Test
{
public:
    Test()
    {
    }
    Test(int num, int count) : m_num(num), m_count(count)
    {
    }
    void thread_func(int a, int b) // 成员函数
    {
        while (1)
        {
            cout << "a = " << a << endl;
            cout << "b = " << b << endl;
            m_count++;
            m_num++;
            sleep(1);
        }
    }

    void thread_func2() // 成员函数
    {
        while (1)
        {
            cout << "num = " << this->m_num << endl;
            cout << "count = " << this->m_count << endl;
            sleep(1);
        }
    }

private:
    int m_num;
    int m_count;
};

class FunctionClass
{
public:
    void operator()(int a, int b)
    {
        while (1)
        {
            cout << "a = " << a << endl;
            cout << "b = " << b << endl;
            sleep(1);
        }
    }
};

class Func
{
public:
    static int my_function(const int &a, const int &b)
    {
        while (1)
        {
            cout << "a = " << a << endl;
            cout << "b = " << b << endl;
            sleep(1);

            return 1;
            // pthread_t id = this->native_handle();
            // pthread_cancel(id);
        }
    }
};

/**
 * 四种形式创建四个线程；分别读4个管道文件数据；
*/

int main(int argc, char **argv)
{

    thread t1(Func::my_function, 1, 2); // 执行单元：普通函数（可调用对象）
    thread t2(Func::my_function, 3, 4);

    int num1 = 2;
    int num2 = 3;
    auto function = [=](int a, int b)
    {
        while (1)
        {   
            cout << "thread di = " << std::this_thread::get_id() << endl;
            cout << "a = " << a + num1 << endl;
            cout << "b = " << b + num2 << endl;
            std::this_thread::sleep_for(std::chrono::seconds(1));
            
        } 
    };

    thread t3(function, 5, 6); // 执行单元:lambda表达式（可调用对象）

    FunctionClass func;
    thread t4(func, 100, 200); // 执行单元:函数对象（可调用对象）

    Test t(10000, 20000);
    // thread t5(&Test::thread_func, t, 1000, 2000);  //执行单元：对象成员函数，对象名 ： 线程不共享对象的成员属性
    // thread t6(&Test::thread_func2, t);

    thread t5(&Test::thread_func, &t, 1000, 2000); // 执行单元：对象成员函数，&对象名 ： 线程共享对象的成员属性
    thread t6(&Test::thread_func2, &t);
     
    t1.detach();
    t2.detach();
    t3.detach();
    t4.detach();
    t5.detach();
    t6.detach();


    pause();

    return 0;